package com.byzk.p2p.admin.score.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.byzk.p2p.admin.base.util.PageInfo;
import com.byzk.p2p.admin.score.dao.ScoreRecordDao;
import com.byzk.p2p.admin.score.dto.ScoreRecord;
import com.byzk.p2p.admin.score.vo.ScoreRecordVo;

@Service
public class ScoreRecordService {
	
	@Autowired
	private ScoreRecordDao scoreRecordDao;
	
	/**
	 * 查询积分流水
	 * @param scoreRecord
	 * @return
	 */
	public PageInfo<ScoreRecordVo> listScoreRecord(ScoreRecordVo scoreRecord){
		int count=scoreRecordDao.countByStatus(scoreRecord);
		List<ScoreRecordVo> voList=scoreRecordDao.listScoreRecord(scoreRecord);
		PageInfo<ScoreRecordVo> page=new PageInfo<ScoreRecordVo>(count, scoreRecord.getIndex(),scoreRecord.getLimit());
		page.setPageList(voList);
		return page;
	}
	
	public ScoreRecord getScoreRecordById(Integer id){
		return scoreRecordDao.getScoreRecordById(id);
	}
	
	public Integer saveScoreRecord(ScoreRecord scoreRecord){
		if(scoreRecord.getId()==null){
			
			return scoreRecordDao.insert(scoreRecord);
		}else{
			return scoreRecordDao.update(scoreRecord);
		}
	}
	
	/**
	 * 积分汇总
	 * @param scoreRecord
	 * @return
	 */
	public ScoreRecordVo scoreRecord(ScoreRecordVo scoreRecord){
		//积分流水查询
		List<ScoreRecordVo> list = scoreRecordDao.listScoreRecord(scoreRecord);
		//总积分查询
		ScoreRecordVo scoreRecordVo = scoreRecordDao.countScore(scoreRecord);
		Integer alreadyScore = 0;//已用积分
			for(ScoreRecordVo score : list){
				if(score.getOptType().equals("out")){
					alreadyScore += score.getAmount();
					scoreRecordVo.setAlreadyScore(alreadyScore);
				}
		}
		//可用积分
		scoreRecordVo.setUsableScore(scoreRecordVo.getScore()-alreadyScore);
		
		return scoreRecordVo;
	}
	
	/**
	 * 查询用户当天是否有抽奖流水
	 */
	public List<ScoreRecord> selectByWay(Integer userId,String way){
		return scoreRecordDao.selectByWay(userId,way);
	}
	
	public List<ScoreRecord> selectByWay2(Integer userId,String way){
		return scoreRecordDao.selectByWay2(userId,way);
	}
	
}
